package cn.bytepath.org.controller;

import cn.bytepath.org.domain.Employee;
import cn.bytepath.org.query.EmployeeQuery;
import cn.bytepath.org.service.IEmployeeService;
import cn.bytepath.util.PageInfo;
import cn.bytepath.util.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 员工的controller层
 * @author Jack.C
 * @createDate 2022-01-23
 * @version 1.0
 */
@Api(tags = "员工接口",description = "员工接口详细描述")
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    /**
     * 注入员工业务层对象
     */
    @Autowired
    private IEmployeeService employeeService;

    @ApiOperation("查询所有employee对象")
    @GetMapping
    public List<Employee> findAll() {
        return employeeService.findAll();
    }

    @ApiOperation("高级查询并分页")
    @PostMapping
    public PageInfo<Employee> pageQuery(@RequestBody EmployeeQuery query) {
        return employeeService.findPageInfoByQuery(query);
    }

    @ApiOperation("根据id删除员工对象")
    @DeleteMapping("/{id}")
    public PageResult delete(@PathVariable("id") Long id) {

        try {
            employeeService.delete(id);
            return PageResult.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return PageResult.FAIL.setMsg(e.getMessage());
        }
    }

    @ApiOperation("根据id批量删除员工对象")
    @PatchMapping
    public PageResult deleteBatch(@RequestBody Long[] ids) {

        try {
            employeeService.deleteBatch(ids);
            return PageResult.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return PageResult.FAIL.setMsg(e.getMessage());
        }
    }

    @ApiOperation(value = "员工添加或修改",notes = "如果有id就是修改否则就是添加")
    @PutMapping
    public PageResult saveOrUpdate(@RequestBody Employee employee) {
        try {
            employeeService.saveOrUpdate(employee);
            return PageResult.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return PageResult.FAIL.setMsg(e.getMessage());
        }
    }
}
